package com.example.wj.controller;

import com.example.wj.entity.Category;
import com.example.wj.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/add")
    public ResponseEntity<Category> add(Category category) {
        return ResponseEntity.ok(this.categoryService.insert(category));
    }

    @PostMapping("/edit")
    public ResponseEntity<Category> edit(Category category) {
        return ResponseEntity.ok(this.categoryService.update(category));
    }

    @PostMapping("/delete")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.categoryService.deleteById(id));
    }

    @GetMapping("{id}")
    public ResponseEntity<Category> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.categoryService.queryById(id));
    }

    @GetMapping("/page")
    public ResponseEntity<Page<Category>> queryByPage(Category category, PageRequest pageRequest) {
        return ResponseEntity.ok(this.categoryService.queryByPage(category, pageRequest));
    }
}

